#!/usr/bin/env python3

# Copyright (c) 2020-2021 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import os
import copy
import platform
from .._util import Util
from .._game_straits import GameStraitForLinux
from .._runner import RunnerBase


class Direct(RunnerBase):

    def __init__(self, verbose=0):
        super().__init__()
        self._verbose = verbose

    def _initialize(self, sys_info, game_id, game_obj):
        super()._initialize(sys_info, game_id, game_obj)

        # check game_obj
        assert game_obj.has_strait(GameStraitForLinux)

        # check game_object.spec
        #   archs:            means CPU architecture of the game binary, must exist as list<str>, element value should be the return value of platform.machine(), for example "x86_64".
        assert hasattr(game_obj.spec, "archs")
        if hasattr(game_obj.spec, "languages"):
            # FIXME
            pass
        if hasattr(game_obj.spec, "default_language"):
            # FIXME
            pass
        if hasattr(game_obj.spec, "resolutions"):
            # FIXME
            pass
        if hasattr(game_obj.spec, "can_fullscreen"):
            # FIXME
            pass
        if hasattr(game_obj.spec, "can_windowed"):
            # FIXME
            pass

        self._gameObj = game_obj

    def _prepare(self, context):
        self._context = context
        self._batchCall("_prepare", self._context)

        self._context.arch = platform.machine()

    def _processGameHint(self, hint):
        assert hint is None

    async def _run(self, item):
        # make sure all the modifications are saved
        self._gameObj._commit()

        runInfo = self._gameObj._getRunInfo(item)
        if True:
            cmdList = [runInfo.exectuable]

            envDict = copy.copy(os.environ)
            envDict.update(runInfo.extra_environemnt_variables)

            await Util.subprocess_exec_check_call(*cmdList)

    def _cleanup(self, on_error=False):
        if hasattr(self, "_context"):
            self._batchCall("_cleanup", self._context, on_error=on_error)
            del self._context
